package com.future.baselib.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by jniu on 2017/5/19.
 */
public class ImageUtils {
    private static long lastClickTime;

    /**
     * 获取 base字符串
     * @param imgPath
     * @return
     */
    public static String getImg(String imgPath) {
        File file = new File(imgPath);
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            data = bitmap2Byte(bitmap);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        return Base64.encode(data);// 返回Base64编码过的字节数组字符串
    }

    public static String getImg(Bitmap bitmap){
        byte[] data = bitmap2Byte(bitmap);
        return Base64.encode(data);
    }

    public static String getImgs(List<String> imgPaths){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < imgPaths.size(); i++) {
            stringBuilder.append(ImageUtils.getImg(imgPaths.get(i))+",");
        }
        return stringBuilder.toString();
    }

    public static byte[] bitmap2Byte(Bitmap bitmap){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,50,baos);
        byte[] bytes = baos.toByteArray();
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    /**
     * 获取 base字符串
     * @param
     * @return
     */
    public static String encryptBase64(Context context,String path,String tip) {

        File file = new File(path);
        if (!file.exists()){
            Toast.makeText(context,tip,Toast.LENGTH_SHORT).show();
            return "";
        }
        ByteArrayOutputStream baos = null;
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(file);
            baos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n;
            while (-1 != (n = in.read(buf))){
                baos.write(buf, 0, n);
            }
            data = baos.toByteArray();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        // 对字节数组Base64编码
        return Base64.encode(data);// 返回Base64编码过的字节数组字符串
    }


    public static String fileToBase64(Context context,String path,String tip) {
        File file = new File(path);
        if (!file.exists()){
            Toast.makeText(context,tip,Toast.LENGTH_SHORT).show();
            return "";
        }
        String base64 = null;
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            byte[] bytes = new byte[in.available()];
            int length = in.read(bytes);
            base64 = android.util.Base64.encodeToString(bytes, 0, length, android.util.Base64.DEFAULT);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return base64;
    }

}
